package com.sky.service.impl;


import com.sky.mapper.ShoppingCartMapper;
import com.sky.vo.PriseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Component
public class ShoppingPriceMaxService {
    public BigDecimal PRICE_MAX;

    @Value("${ShoppingCart.priceMax}")
    private BigDecimal priceMax;
    @javax.annotation.PostConstruct
    public void init() {
        PRICE_MAX = priceMax;
        log.info("对ShoppingPriceMAXUtil工具类的最大值进行复制操作！"+priceMax);
    }

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 计算用户购物车物品总额！！
     * @param userId
     * @param price
     * @return
     */
    public boolean AddPriceMAX(Long userId, BigDecimal price) {
        BigDecimal bd  = Price(userId);
        log.info("用户购物车物品总额：{}",bd);
        bd = bd.add(price);
        log.info("用户购物车金额总额：{}",bd);
        if (bd.compareTo(PRICE_MAX) > 0) {
            return false;
        }else {
            return true;
        }
    }
    public BigDecimal Price(Long userId){
        List<PriseVO> priseVOS = shoppingCartMapper.settleAccounts(userId);
        BigDecimal bd  = new BigDecimal("0");
        for (PriseVO priseVO : priseVOS) {
            bd = bd.add(priseVO.getAmount().multiply(new BigDecimal(priseVO.getNumber())));
        }
        return bd.setScale(2);
    }
}
